/**
 * 该js文件是常用的微信小程序的工具
 */
const filemgr = wx.getFileSystemManager();
const targetPath = wx.env.USER_DATA_PATH;
const wxUtils = {
	/**
	 * 下载文件并保存工具
	 * @param {Object} url 文件路径
	 */
	downloadAndSaveFile(url, lockId) {
		return new Promise((resolve, reject) => {
			wx.downloadFile({
				url: url,
				success: function(res) {
					console.log('下载res', res)
					filemgr.saveFile({
						tempFilePath: res.tempFilePath,
						filePath: targetPath + "/" + lockId + ".zip",
						success(result) {
							resolve({
								"lockId": lockId,
								"url": result.savedFilePath
							})
						},
						fail(err) {
							console.error('saveFile err-->', err)
							reject(err)
						}
					})
				},
				fail(err) {
					console.error('downloadFile err-->', err)
					reject(err)
				}
			})
		})
	},
	/**
	 * 根据lockId解压相关压缩包
	 * @param {Object} lockId
	 */
	unzip(lockId) {
		return new Promise((resolve, reject) => {
			filemgr.unzip({
				zipFilePath: targetPath + '/' + lockId + '.zip',
				targetPath: targetPath,
				success(unzipRes) {
					resolve(unzipRes);
				},
				fail(err) {
					reject(err);
				}
			})
		})
	},
	readFile(url, encoding = '') {
		return new Promise((resolve, reject) => {
			filemgr.readFile({
				filePath: targetPath + '/' + url,
				encoding,
				success(res) {
					resolve(res)
				},
				fail(err) {
					reject(err)
				}
			})
		})
	}




}
export default wxUtils;